package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.*;
import person.aoao.library.entity.UserList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BorrowListService;
import person.aoao.library.service.RoleListService;
import person.aoao.library.service.UserListService;
import person.aoao.library.service.WillListService;
import person.aoao.library.vo.HomeData;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 用户表(UserList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:40
 */
@Service("userListService")
public class UserListServiceImpl implements UserListService {
    @Resource
    private UserListDao userListDao;
    @Resource
    private RoleListService roleListService;

    @Resource
    private BorrowListService borrowListService;

    @Resource
    private WillListService willListService;

    @Resource
    private SeatListDao seatListDao;

    @Resource
    private BookListDao bookListDao;

    @Resource
    private CommentListDao commentListDao;

    @Resource
    private WillListDao willListDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserList queryById(Integer id) {
        return this.userListDao.queryById(id);
    }



    /**
     * 分页查询
     *
     * @param userList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<UserList> queryByPage(UserList userList, PageRequest pageRequest) {
        long total = this.userListDao.count(userList);
        return new PageImpl<>(this.userListDao.queryAllByLimit(userList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param userList 实例对象
     * @return 实例对象
     */
    @Override
    public UserList insert(UserList userList) {
        if(userList.getPassword()==null){
            userList.setPassword("123");
        }
        Integer bool=this.userListDao.selectByNumber(userList.getPhonenumber());
        if(bool<1){
            if(userList.getRoleid()==null){
                userList.setRoleid("81903");
            }
            if(userList.getState()==null){
                userList.setState(1);
            }

            Integer info=this.userListDao.insert(userList);
            System.out.println(info);
            if(info>0){
                String id=userList.getId()<10?"0"+userList.getId():userList.getId().toString();
                userList.setUid((int)(Math.random()*900 + 100)+id+(int)(Math.random()*900 + 100)+userList.getRoleid().charAt(3));
                System.out.println(userList.getUid());
                this.userListDao.update(userList);
            }
            return userList;

        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param userList 实例对象
     * @return 实例对象
     */
    @Override
    public UserList update(UserList userList) {
        Integer i=this.userListDao.update(userList);
        System.out.println(userList.getUid());
        System.out.println(i);
        return this.queryById(userList.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userListDao.deleteById(id) > 0;
    }

    @Override
    public UserList reqLogin(String uid, String password) {
        boolean bool= Pattern.matches("^1[3-9]\\d{9}$",uid);
        UserList reader=null;
        if(bool){
            reader=userListDao.reqLoginByPhoneNumber(uid,password);
        }else{
            reader=userListDao.reqLogin(uid,password);
        }
        if(reader==null){
            return null;
        }else{
            reader.setPassword(null);
            if(Integer.parseInt(reader.getRoleid())!=81903){
                reader.setRoleList(this.roleListService.queryById(Integer.parseInt(reader.getRoleid())));
                return  reader;
            }else{
                return reader;
            }
        }
    }

    @Override
    public Integer updateReader(String uid, UserList reader) {
        System.out.println(uid);
        Integer bool=userListDao.updateReader(uid,reader);
        return bool;
    }



    @Override
    public ListData getUesrList(String type, Integer num, Integer page) {
        List<UserList> list=this.userListDao.getUserList(num,(page-1)*num);
        long count=this.userListDao.getCount();
        ListData dataList=new ListData();
        dataList.setData(list);
        if(type.equals("admin")){
            for(UserList item:list){
                item.setRoleList(this.roleListService.queryById(Integer.parseInt(item.getRoleid())));
            }
            List<UserList> roleList=list.stream().filter(item->!item.getRoleid().equals("81903")).collect(Collectors.toList());
            count=this.userListDao.getCountByAdmin();
            dataList.setData(roleList);
            dataList.setCount(count);
            return dataList;
        }else if(type.equals("reader")){
            dataList.setData(list);
            dataList.setCount(count);
            return  dataList;
        }
        return null;
    }

    @Override
    public ResponseData checkInfo(String type, String value) {
        if(type.equals("phoneNumber")){
            if(this.userListDao.selectByNumber(value)>0){
                return new ResponseData(ResponseCode.Number_ERROR);
            }
        }else if(type.equals("mail")){
            if(this.userListDao.selectByMail(value)>0){
                return new ResponseData(ResponseCode.Mail_ERROR);
            }
        }else if(type.equals("")){
            return new ResponseData(ResponseCode.ERROR);
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @Override
    public UserList getUserInfoByUid(String uid) {
        UserList info=this.userListDao.getUserInfoByUid(uid);
        return  info;
    }

    @Override
    public ResponseData deleteUserInfo(Integer id) {
        Integer num=this.userListDao.deleteById(id);
        if(num>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return  new ResponseData(ResponseCode.ERROR);
        }
    }

    @Override
    public ListData searchUser(String value,String type,Integer num,Integer page) {
        List<UserList> list=this.userListDao.searchUser("%"+value+"%",(page-1)*num,num);
        long count=this.userListDao.searchCount("%"+value+"%");
        ListData dataList=new ListData();
        if(type.equals("admin")){
            list=list.stream().filter(item->!item.getRoleid().equals("81903")).collect(Collectors.toList());
            dataList.setData(list);
            count=this.userListDao.getCountByAdmin();
            dataList.setCount(count);
            return dataList;
        }else{
            dataList.setData(list);
            dataList.setCount(count);
            return dataList;
        }
    }

    @Override
    public ResponseData addAdmin(String uid, Integer roleid) {
        UserList info=this.userListDao.queryByUid(uid);
        if(info==null){
            return new ResponseData(ResponseCode.Acount_NO);
        }
        else if(info.getRoleid().equals("81903")){
            info.setRoleid(roleid+"");
            this.userListDao.update(info);
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.Acount_ERROR);
    }

    @Override
    public ResponseData getUserDetail(String uid) {
        UserList baseInfo=this.userListDao.queryByUid(uid);
        baseInfo.setRoleList(this.roleListService.queryById(Integer.parseInt(baseInfo.getRoleid())));
        return  new ResponseData(ResponseCode.SUCCESS,baseInfo);
    }

    @Override
    public ResponseData editPaawd(UserList userList) {
        UserList user=this.userListDao.getUserInfoByUid(userList.getUid());
        System.out.println("原密码："+user.getPassword());
        System.out.println("输入旧密码："+userList.getPassword());
        System.out.println("输入的新密码："+userList.getRepassword());
        if(user.getPassword().equals(userList.getPassword())){
            System.out.println("修改");
            user.setPassword(userList.getRepassword());
            this.userListDao.update(user);
            return new ResponseData(ResponseCode.SUCCESS);

        }else{
            return new ResponseData(ResponseCode.Acount_ERROR);
        }
    }

    @Override
    public ResponseData getHomePage() {
        ArrayList<HomeData> home=new ArrayList<>(10);
        String titles[]=new String[]{"用户总人数","图书总数","座位总数","评论总数","预约总数"};
        long userCount=this.userListDao.getCount();
        long bookCount=this.bookListDao.getBookListCount();
        long seatCount=this.seatListDao.getCount();
        long comCount=this.commentListDao.getCount();
        long willCount=this.willListDao.getCount();
        long counts[]=new long[]{userCount,bookCount,seatCount,comCount,willCount};
        for(int i=0;i<titles.length;i++){
            HomeData item=new HomeData();
            item.setTitle(titles[i]);
            item.setCount(counts[i]);
            home.add(i,item);
        }
        return new ResponseData(ResponseCode.SUCCESS,home);
    }


}
